<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="30%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="供应商" prop="supplierId">
      <el-select v-model="dataForm.supplierId" placeholder="选择供应商">
        <el-option 
          v-for="item in dataForm.supplierList"
          :key="item.supplierId"
          :label="item.supplierName"
          :value="item.supplierId">
        </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="类型" prop="budgetType">
      <el-select v-model="dataForm.budgetType" placeholder="选择收支类型">
        <el-option label="收入" value="1" ></el-option>
        <el-option label="支出" value="0" ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="金额" prop="money">
      <el-input v-model="dataForm.money" placeholder="金额" style="width:200px"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remarks">
      <el-input type="textarea" v-model="dataForm.remarks" placeholder="备注" ></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dialogVisible: false,
        dataForm: {
          supplierList: [],
          supplierId: '',
          money: '',
          budgetType: '',
          remarks: ''
        },
        dataRule: {
          supplierId: [
            { required: true, message: '供应商不能为空', trigger: 'blur' }
          ],
          budgetType: [
            { required: true, message: '收支类型不能为空', trigger: 'blur' }
          ],
          money: [
            { required: true, message: '金额不能为空', trigger: 'blur' },
            { pattern: /^(0|[1-9]\d*)(\.\d{1,2})?$/, message: '数据不合法', trigger: 'blur' }
          ],
          remarks: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init () {
        this.visible = true
        this.$nextTick(() => {
          // 查询现有供应商
          this.$http({
            url: this.$http.adornUrl('/sys/user/querySupplierList'),
            method: 'get'
          }).then(({data}) => {
            this.dataForm.supplierList = data.data
          })
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/user/addSupplierBill`),
              method: 'post',
              data: this.$http.adornData({
                'sysUserId': this.dataForm.supplierId,
                'money': this.dataForm.money,
                'budgetType': this.dataForm.budgetType,
                'remarks': this.dataForm.remarks
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
